package com.recruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recruit.model.InterviewDO;
import com.recruit.model.result.InterviewResultDO;
import com.recruit.service.InterviewService;
import com.recruit.vo.PageResponseVO;
import com.recruit.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * @author DHF
 */
@RestController
@RequestMapping("/recruit/interview")
@PermissionModule(value = "面试")
@Api(tags = "面试操作接口")
public class InterviewController {

    @Resource
    private InterviewService interviewService;

    @GetMapping("/page/{hrId}")
    @GroupRequired
    @PermissionMeta(value = "面试管理")
    @ApiOperation(value = "面试管理-列表查看")
    public PageResponseVO<InterviewResultDO> page(
            @ApiParam(value = "页面条数", required = false)
            @RequestParam(name = "count", required = false, defaultValue = "10") @Min(value = 1, message = "{page" +
                    ".count.min}") @Max(value = 30, message = "{page.count.max}") Integer count,
            @ApiParam(value = "查询页", required = false)
            @RequestParam(name = "page", required = false, defaultValue = "0") @Min(value = 0, message = "{page" +
                    ".number.min}") Integer page,
            @ApiParam(value = "hrrId", required = true)
            @PathVariable(value = "hrId") @Positive(message = "{id.positive}") Integer hrId,
            @ApiParam(value = "面试状态", required = true)
            @RequestParam Integer state) {
        return interviewService.getByHrId(count, page, hrId);
    }

    @GetMapping("/page/find/{userId}")
    @GroupRequired
    @PermissionMeta(value = "我的面试")
    @ApiOperation(value = "我的面试")
    public PageResponseVO<InterviewResultDO> pageByUserId(
            @ApiParam(value = "页面条数", required = false)
            @RequestParam(name = "count", required = false, defaultValue = "10") @Min(value = 1, message = "{page" +
                    ".count.min}") @Max(value = 30, message = "{page.count.max}") Integer count,
            @ApiParam(value = "查询页", required = false)
            @RequestParam(name = "page", required = false, defaultValue = "0") @Min(value = 0, message = "{page" +
                    ".number.min}") Integer page,
            @ApiParam(value = "userId", required = true)
            @PathVariable(value = "userId") @Positive(message = "{id.positive}") Integer userId,
            @ApiParam(value = "面试状态", required = true)
            @RequestParam Integer state) {
        return interviewService.getByUserId(count, page, userId);
    }

    @PostMapping("/sendInterviewNotify")
    @GroupRequired
    @PermissionMeta(value = "面试管理")
    @ApiOperation(value = "发送面试管理信息")
    public UpdatedVO sendInterviewNotify(
            @ApiParam(value = "面试信息实体", required = true)
            @RequestBody InterviewDO interviewDO) {
        QueryWrapper<InterviewDO> wrapper = new QueryWrapper<>();
        wrapper.eq("id", interviewDO.getId());
        InterviewDO result = interviewService.getOne(wrapper);
        if (result == null) {
            throw new NotFoundException(41000);
        }
        result.setStatus(1);
        result.setAddress(interviewDO.getAddress());
        result.setMemo(interviewDO.getMemo());
        result.setTime(interviewDO.getTime());
        interviewService.updateById(result);
        return new UpdatedVO(4100);
    }

    @PostMapping("/updateInterviewResult")
    @GroupRequired
    @PermissionMeta(value = "面试管理")
    @ApiOperation(value = "修改面试管理")
    public UpdatedVO updateInterviewResult(
            @ApiParam(value = "面试信息实体", required = true)
            @RequestBody InterviewDO interviewDO) {
        QueryWrapper<InterviewDO> wrapper = new QueryWrapper<>();
        wrapper.eq("id", interviewDO.getId());
        InterviewDO result = interviewService.getOne(wrapper);
        if (result == null) {
            throw new NotFoundException(41000);
        }
        result.setStatus(interviewDO.getStatus());
        result.setComments(interviewDO.getComments());
        interviewService.updateById(result);
        return new UpdatedVO(4100);
    }
}
